package com.suray.wcs.service.system.scheduled;

import com.suray.basic.wcs.plc.service.IPlcLogService;
import com.suray.commin.core.utils.SpringContextHolder;
import com.suray.system.run.seer.mysql.SeerTaskDBServiceImpl;
import com.suray.wcs.quartz.service.IJobLogDBService;
import com.suray.wcs.service.system.entity.TaskDB;
import com.suray.wcs.service.system.service.*;
import com.suray.wcs.service.system.service.impl.AgvTaskDBServiceImpl;
import com.suray.wcs.service.system.service.impl.SysLogininforServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务调度：定时清理日志
 *
 * @author shaozn
 */
@Component("deleteLogTask")
public class DeleteLogTask {
    private Logger logger = LoggerFactory.getLogger(DeleteLogTask.class);
    /**
     * 删除小车心跳日志
     */
    public void deleteRgvHeartLog(Integer days) {
        try {
            //获取bean
            IRgvHeartLogService rgvHeartLogService = SpringContextHolder.getBean(IRgvHeartLogService.class);
            //执行任务
            rgvHeartLogService.deleteThreeDaysAgoLog(days);
        } catch (Exception e) {
            logger.error("deleteRgvHeartLog执行失败: " + e.getLocalizedMessage());
        }
    }

    /**
     * 删除PLC日志
     */
    public void deletePlcLog(Integer days){
        try {
            //获取Bean
            IPlcLogService plcLogService = SpringContextHolder.getBean(IPlcLogService.class);
            //执行任务
            plcLogService.deletePlcLog(days);
        }catch (Exception e){
            logger.error("deletePlcLog执行失败："+ e.getLocalizedMessage());
        }
    }

    /**
     * 刪除小车日志
     */
    public void deleteRgvLog(Integer days){
        try {
            //获取Bean
            RgvLogService rgvLogService = SpringContextHolder.getBean(RgvLogService.class);
            //执行任务
            rgvLogService.deleteThreeDaysAgoLog(days);
        }catch (Exception e){
            logger.error("deleteRgvLog执行失败：" + e.getLocalizedMessage());
        }
    }

    /**
     * 删除操作日志清理任务
     */
    public void deleteInterfaceLog(Integer days) {
        try {
            //获取bean
            IInterfaceLogService iInterfaceLogService = SpringContextHolder.getBean(IInterfaceLogService.class);
            //执行任务
            iInterfaceLogService.deleteThreeDaysIfLog(days);
        } catch (Exception e) {
            logger.error("deleteInterfaceLog执行失败: " + e.getLocalizedMessage());
        }
    }

    /**
     * 异常日志清理任务
     */
    public void deleteExceptionLog(Integer days) {
        try {
            //获取bean
            IExceptionLogdbService iexceptionLogdbService = SpringContextHolder.getBean(IExceptionLogdbService.class);
            //执行任务
            iexceptionLogdbService.deleteThreeDaysEpLog(days);
        } catch (Exception e) {
            logger.error("deleteExceptionLog执行失败: " + e.getLocalizedMessage());
        }
    }

    /**
     * 清除系统操作日志定时任务
     */
    public void deleteSysOperLog(Integer days) {
        ISysLogService sysLogService = SpringContextHolder.getBean(ISysLogService.class);
        try {
            //执行任务
            sysLogService.deleteSysLog(days);
        } catch (Exception e) {
            logger.error("delete SysLog执行失败: " + e.getLocalizedMessage());
        }
        try {
            //执行任务
            sysLogService.deleteSysOperLog(days);
        } catch (Exception e) {
            logger.error("delete SysOperLog执行失败: " + e.getLocalizedMessage());
        }
        try {
            IJobLogDBService jobLogDBService = SpringContextHolder.getBean(IJobLogDBService.class);
            jobLogDBService.deleteJobLog(days);
        } catch (Exception e) {
            logger.error("delete jobLog执行失败: " + e.getLocalizedMessage());
        }
        try {
            SysLogininforServiceImpl logininforService = SpringContextHolder.getBean(SysLogininforServiceImpl.class);
            logininforService.deleteByTime(days);
        } catch (Exception e) {
            logger.error("delete logininfor执行失败: " + e.getLocalizedMessage());
        }
    }

    /**
     * 清除小铃铛定时任务
     */
    public void deleteNotification(Integer days) {
        try {
            //获取bean
            INotificationService notificationService = SpringContextHolder.getBean(INotificationService.class);
            //执行任务
            notificationService.deleteNotification(days);
        } catch (Exception e) {
            logger.error("deleteNotification执行失败: " + e.getLocalizedMessage());
        }
    }

    /**
     * 清除任务列表定时任务
     */
    public void deleteTask(Integer days) {
        try {
            //获取bean
            ITaskDBService taskdbService = SpringContextHolder.getBean(ITaskDBService.class);
            TaskDetailService taskDetailService = SpringContextHolder.getBean(TaskDetailService.class);
            List<TaskDB> taskDBS = taskdbService.queryTaskForDays(days);
            List<Integer> taskIds = taskDBS.stream().map(TaskDB::getTaskId).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(taskIds)) {
                taskDetailService.deleteTaskDetailByTaskId(taskIds);
            }
            //执行任务
            taskdbService.deleteTaskForDays(days);
            logger.info("DeleteTaskThread执行成功");
        } catch (Exception e) {
            logger.error("DeleteTaskThread执行失败: ", e);
        }

        deleteAgvTask(days);
    }

    public void deleteAgvTask(Integer days) {
        try {
            SeerTaskDBServiceImpl seerTaskDBService = SpringContextHolder.getBean(SeerTaskDBServiceImpl.class);
            seerTaskDBService.deleteByTime(days);
            logger.info("DeleteTaskThread seer task 执行成功");
        } catch (BeansException e) {
            logger.error("DeleteTaskThread seer task 执行失败: ", e);
        }

        try {
            AgvTaskDBServiceImpl agvTaskDBService = SpringContextHolder.getBean(AgvTaskDBServiceImpl.class);
            agvTaskDBService.deleteTaskForDays(days);
            logger.info("DeleteTaskThread agv task 执行成功");
        } catch (BeansException e) {
            logger.error("DeleteTaskThread agv task 执行失败: ", e);
        }
    }

    /**
     * 定时清除非出入移任务
     */
    public void deleteManualTask(Integer days) {
        try {
            ITaskDBService taskdbService = SpringContextHolder.getBean(ITaskDBService.class);
            TaskDetailService taskDetailService = SpringContextHolder.getBean(TaskDetailService.class);
            List<TaskDB> taskDBS = taskdbService.queryManualTaskForDays(days);
            List<Integer> taskIds = taskDBS.stream().map(TaskDB::getTaskId).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(taskIds)) {
                taskDetailService.deleteTaskDetailByTaskId(taskIds);
            }
            taskdbService.deleteManualTaskForDays(days);
            logger.info("deleteManualTask执行成功");
        } catch (Exception e) {
            logger.error("deleteManualTask执行失败: ", e);
        }
    }


    /**
     * 删除
     * @param params
     */
    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        //获取bean
    }
}
